package pin.db.schema.base;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class BasePeer {
    public static void list(PrintStream out, Object object) throws Exception {
        Class cls=object.getClass();
        Method[] methods = cls.getMethods();
        
        for (int i = 1; i < methods.length; i++) {
            if (methods[i].getName().startsWith("get") && !methods[i].getName().equals("getClass") && methods[i].getParameterTypes().length<1) {
                out.println(methods[i].getName().substring(3) + ":" + methods[i].invoke(object));
            }
        }
    }
    public static ResultSet executeQuery(String sql, Connection conn) throws SQLException {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        return rs;
    }
    public static ResultSet executeQuery(String tableName, String[] columns, String conditions, Object[] values, int offset, int limit,Connection conn) throws SQLException {
        StringBuffer columnsString = new StringBuffer();
        for (String column : columns) {
            columnsString.append(",`" + tableName + "`.`" + column + "`");
        }
        columnsString.deleteCharAt(0);
        String sql = String.format("SELECT %s FROM `%s` WHERE %s LIMIT %d,%d ;",
                columnsString.toString(), tableName, conditions,offset,limit);
        System.out.println(sql);
        PreparedStatement pstmt = conn.prepareStatement(sql);
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                //values[i].getClass().getSimpleName();
                pstmt.setObject(i + 1, values[i]);
            }
        }
        ResultSet rs = pstmt.executeQuery();
        return rs;
    }
}
